package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.pojo.SysNotice;
import com.cl.short_video_java.pojo.VideoUser;
import com.cl.short_video_java.service.SysNoticeService;
import com.cl.short_video_java.mapper.SysNoticeMapper;
import com.cl.short_video_java.service.VideoUserService;
import com.cl.short_video_java.util.StringUtils;
import com.cl.short_video_java.vo.sys.Notice;
import com.cl.short_video_java.vo.sys.NoticeSearch;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author object
 * @description 针对表【sys_notice】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice>
		implements SysNoticeService {

	private VideoUserService videoUserService;

	SysNoticeServiceImpl(VideoUserService videoUserService) {
		this.videoUserService = videoUserService;
	}

	/**
	 * 分页查询通知列表
	 *
	 * @param noticeSearch 查询对象
	 * @return
	 */
	@Override
	public Page<SysNotice> page(NoticeSearch noticeSearch) {
		System.out.println(StringUtils.isEmpty(noticeSearch.getUsername()) + "==" + StringUtils.isEmpty(noticeSearch.getTitle()));
		Page<SysNotice> page = this.page(new Page<SysNotice>(noticeSearch.getPageNum(), noticeSearch.getPageSize()),
				new QueryWrapper<SysNotice>()
						.like(!StringUtils.isEmpty(noticeSearch.getTitle()), "title", noticeSearch.getTitle())
						.like(!StringUtils.isEmpty(noticeSearch.getUsername()), "oper_user", noticeSearch.getUsername())
						.eq(noticeSearch.getType() != null, "type", noticeSearch.getType())
						.orderByDesc("create_time"));
		return page;
	}

	/**
	 * 添加公告
	 *
	 * @param notice
	 * @param request
	 */
	@Override
	public void insert(Notice notice, HttpServletRequest request) {
		VideoUser user = videoUserService.getCurrentUser(request, Const.GRADE_ADMIN);
		Assert.isTrue(user.getIsAdmin() == 1, "抱歉，权限不足");
		SysNotice sysNotice = new SysNotice();
		if (notice.getId() != null) {
			sysNotice.setId(notice.getId());
		}
		sysNotice.setCreateTime(LocalDateTime.now());
		sysNotice.setOperUser(user.getUsername());
		sysNotice.setTitle(notice.getTitle());
		sysNotice.setContent(notice.getContent());
		sysNotice.setUpdateTime(LocalDateTime.now());
		sysNotice.setType(notice.getType() != null ? notice.getType() : 0);
		sysNotice.setStatus(notice.getStatus() != null ? notice.getStatus() : 0);
		this.saveOrUpdate(sysNotice);
	}

	@Override
	public void del(List<Integer> ids) {
		System.out.println(ids);
		this.removeByIds(ids);
	}
}




